package com.ll.demo;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Scanner;

/**
 * @author LL
 */
public class SmallChanageSys {
    public static void main(String[] args) {
        //化繁为简
        //1.先完成显示菜单,并可以选择菜单,给出对应提示

        //定义相关变量
        //退出字段
        boolean loop = true;
        //存储用户输入
        String key = "";
        Scanner scanner = new Scanner(System.in);

        // 格式化时间
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        //完成收益入账
        double money = 0;
        double balance = 0;

        //消费
        //定义变量,保存消费的原因
        String note = "";

        //完成零钱明细
        //1.可以把收益入账和消费,保存到数组;
        //2.可以使用对象
        //3.可以使用Stirng拼接
        String details = "---------------零钱通明细如下:-------------\n";
        do {
            // 显示菜单
            System.out.println("===============零钱通菜单==========");
            System.out.println("1 零钱通明细");
            System.out.println("2 收益入账");
            System.out.println("3 消费");
            System.out.println("4 退     出");

            //提示用户属于
            System.out.println("请选择(1-4)");
            key = scanner.next();

            //使用switch分支控制
            switch (key) {
                case "1":
                    //System.out.println("1 零钱通明细");
                    System.out.println(details);
                    break;
                case "2":
                    System.out.print("2 收益入账金额: ");
                    money = scanner.nextDouble();
                    //money 的值范围应该校验
                    if (money <= 0) {
                        System.out.println("收益入账金额需要大于0");
                        break;
                    }
                    System.out.print("收益说明: ");
                    note = scanner.next();
                    balance += money;
                    //拼接收益入账信息到 details
                    details += note + "\t+" + money + "\t" + dateTimeFormatter.format(LocalDateTime.now())
                            + "\t余额: " + balance + "\n";
                    System.out.println(details);
                    break;
                case "3":
                    System.out.print("消费金额: ");
                    money = scanner.nextDouble();
                    if (money <= 0) {
                        System.out.println("错误的消费,消费金额应该大于0");
                        break;
                    } else if (money > balance) {
                        System.out.println("错误的消费,消费金额大余额");
                        break;
                    }
                    System.out.print("消费说明: ");
                    note = scanner.next();
                    balance -= money;
                    details += note + "\t-" + money + "\t" + dateTimeFormatter.format(LocalDateTime.now()) +
                            "\t余额: " + balance + "\n";
                    System.out.println(details);
                    break;
                case "4":
                    String choice = "";
                    while (true) {
                        System.out.println("你确定要退出吗? y/n");
                        choice = scanner.next();
                        if ("y".equals(choice) || "n".equals(choice)) {
                            break;
                        }
                    }

                    // 当用户输入的是y,退出switch语句和while语句
                    if ("y".equals(choice)) {
                        loop = false;
                    }
                    break;
                default:
                    System.out.println("选择有误 请重新选择");
                    break;
            }
        } while (loop);

        System.out.println("------退出了零钱通项目------");

    }
}
